package ModelLayer;
import java.util.*;


/**
 * Write a description of class DVDregister here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class DVDregister
{
    // instance variables - replace the example below with your own
    private static DVDregister _instance = null;
    private HashSet<DVD> dvdRegister;
    
    public static DVDregister getInstance()
    {
        if(null == _instance) 
        {
            _instance = new DVDregister();
        }
        return _instance;
    }

    public void readLoanTitle(String title)
    {
        Iterator<DVD> it=dvdRegister.iterator();
        while(it.hasNext())
        {
            DVD dvd=it.next();
            if(dvd.readTitle().equals(title))
            {
                Iterator<Copy> it2=dvd.readCopies().iterator();
                while(it2.hasNext())
                {
                    Copy copy=it2.next();
                    Iterator<Loan> it3=copy.readLoans().iterator();
                    while(it3.hasNext())
                    {
                        Loan loan=it3.next();
                        System.out.println("***************");
                        System.out.println("Person name: "+loan.readPerson().readName());
                        System.out.println("***************");
                        System.out.println("ID: "+loan.readID());
                        System.out.println("BorrowDate: "+loan.readBorrowDate());
                        System.out.println("State: "+loan.readState());
                    }
                }
            }
        }
    }
    
    public void createLoan(Person person,String title, int id, String borrowDate, String period)
    {
        Iterator<DVD> it = dvdRegister.iterator();
        while(it.hasNext()) 
        {
            DVD dvd = it.next();
            if(dvd.readTitle().equals(title))
                {
                    Iterator<Copy> it2 = dvd.readCopies().iterator();
                    while(it2.hasNext())
                    {
                        Copy copy=it2.next();
                        if(copy.checkAvailable())
                        {
                            copy.createLoan(person,id,borrowDate,period);
                            
                        }
                    }
                }
        }
    }
    
    public void returnLoan(String name, String title)
    {
        Iterator<DVD> it = dvdRegister.iterator();
        while(it.hasNext())
        {   
            DVD dvd=it.next();
            if(dvd.readTitle().equals(title))
            {
                Iterator<Copy> it2=dvd.readCopies().iterator();
                while(it2.hasNext())
                {
                    Copy copy=it2.next();
                    Iterator<Loan> it3=copy.readLoans().iterator();
                    while(it3.hasNext())
                    {
                        Loan loan=it3.next();
                        if(loan.readPerson().readName().equals(name))
                        {
                            loan.updateState(true);
                            Iterator<Loan> it4= loan.readPerson().readLoans().iterator();
                            while(it4.hasNext())
                            {
                                Loan loan2=it4.next();
                                if(loan.readID()==loan2.readID())
                                {
                                    loan2.updateState(true);
                                }
                            }
                            System.out.println("RETURNED!");
                        }
                    }
                }
            }
        }
    }
    
    public void deleteLoan(String name, String title)
    {
        Iterator<DVD> it = dvdRegister.iterator();
        while(it.hasNext())
        {   
            DVD dvd=it.next();
            if(dvd.readTitle().equals(title))
            {
                Iterator<Copy> it2=dvd.readCopies().iterator();
                while(it2.hasNext())
                {
                    Copy copy=it2.next();
                    Iterator<Loan> it3=copy.readLoans().iterator();
                    while(it3.hasNext())
                    {
                        Loan loan=it3.next();
                        if(loan.readPerson().readName().equals(name))
                        {
                            copy.readLoans().remove(loan);
                            loan.updateState(true);
                            Iterator<Loan> it4= loan.readPerson().readLoans().iterator();
                            while(it4.hasNext())
                            {
                                Loan loan2=it4.next();
                                if(loan.readID()==loan2.readID())
                                {
                                    loan2.readPerson().readLoans().remove(loan2);
                                    System.out.println("REMOVED!");
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    
    public Copy checkTitle(String title, int id)
    {
        Iterator<DVD> it = dvdRegister.iterator();
        while(it.hasNext()) 
        {
            DVD dvd = it.next();
            if(dvd.readTitle().equals(title))
               {
                   Iterator<Copy> it2 = dvd.readCopies().iterator();
                   while(it2.hasNext())
                       {
                           Copy copy=it2.next();
                           if(copy.checkAvailable())
                                return copy;
                       }
               }
            }
        return null;
    }
    
    
    
    public DVDregister()
    {
        dvdRegister=new HashSet<>();
    }
    
    public boolean checkCopyAvailable(String title)
    {
        Iterator<DVD> it = dvdRegister.iterator();
        while(it.hasNext()) 
        {
            DVD dvd = it.next();
            if(title.equals(dvd.readTitle()))
            {
                Iterator<Copy> it2= dvd.readCopies().iterator();
                while(it2.hasNext())
                {
                    Copy copy=it2.next();
                    if(copy.readSize()==0)
                    {
                        return true;
                    }
                    else
                    {
                        Iterator<Loan> it3 = copy.readLoans().iterator();
                        while(it3.hasNext())
                        {
                            Loan loan=it3.next();
                            if(loan.readState())
                                return true;
                        }
                    }
                }
            }
        }
        return false;
    }
    
     public boolean checkID(int id)
    {
        Iterator<DVD> it = dvdRegister.iterator();
        while(it.hasNext()) 
        {
            DVD dvd = it.next();
            if(id==dvd.readID())
                return true;
        }
        return false;
    }
    
    public int readNumberOfCopies(int id)
    {
        int number=0;
        Iterator<DVD> it = dvdRegister.iterator();
        while(it.hasNext()) 
        {
            DVD dvd = it.next();
            if(id==dvd.readID()) 
            {
               number= dvd.readSize();
            }
        }
        return number;
    }
    
    
    public int readSize()
    {
       return dvdRegister.size();
    }
    
    public boolean checkSerialNumber(int id, int serialNumber)
    {
        Iterator<DVD> it = dvdRegister.iterator();
        while(it.hasNext()) 
        {
            DVD dvd = it.next();
            if(id==dvd.readID())
            {   
                Iterator<Copy> it2= dvd.readCopies().iterator();
                while(it2.hasNext())
                {
                    Copy copy=it2.next();
                    if(serialNumber==copy.readSerialNumber())
                        return true;
                }
            }
        }
        return false;
    }
    
    public void addCopy(int id, int serialNumber, String purchaseDate, int price)
    {
        Iterator<DVD> it = dvdRegister.iterator();
        while(it.hasNext()) 
        {
            DVD dvd = it.next();
            if(id==dvd.readID()) 
            {
                dvd.addCopy(serialNumber,purchaseDate,price);
            }
        }
    }
    
    public void removeCopy(int id,int serialNumber)
    {
        Iterator<DVD> it = dvdRegister.iterator();
        while(it.hasNext()) 
        {
            DVD dvd = it.next();
            if(id==dvd.readID())
            {   
                Iterator<Copy> it2= dvd.readCopies().iterator();
                while(it2.hasNext())
                {
                    Copy copy=it2.next();
                    if(serialNumber==copy.readSerialNumber())
                        dvd.readCopies().remove(copy);
                }
            }
        }
    }

     public void printDVDs()
    {
        int count=1;
        Iterator<DVD> it = dvdRegister.iterator();
        while(it.hasNext()) 
        {
            DVD dvd = it.next();
            System.out.println(count+":");
            System.out.println("ID: "+dvd.readID());
            System.out.println("Title: "+dvd.readTitle());
            count++;
        }
    }
    
    public Boolean addDVD(int id, String title, String artist, String publicationDate)
    {   
        Iterator<DVD> it = dvdRegister.iterator();
        while(it.hasNext()) 
        {
            DVD dvd = it.next();
             if(id==dvd.readID())
            {
                return false;
            }
        }
        DVD newDVD=new DVD();
        newDVD.create(id, title, artist, publicationDate);
        dvdRegister.add(newDVD); 

        return true;
    }
    
    public Boolean removeDVD(int id)
    {
        
        Iterator<DVD> it = dvdRegister.iterator();
        while(it.hasNext()) 
        {
            DVD dvd = it.next();
            if(id==dvd.readID()) 
            {
                dvdRegister.remove(dvd);
                System.out.println("DELETED");
                return true;
            }
        }
        return false;
        
    }
    
    public void updateDVDID(int id,int newID)
    {
        DVD dvd=returnDVD(id);
        dvd.updateID(newID);
    }
    
    public void updateDVDTitle(int id,String title)
    {
        DVD dvd=returnDVD(id);
        dvd.updateTitle(title);
    }
    
    public void updateDVDArtist(int id,String artist)
    {
        DVD dvd=returnDVD(id);
        dvd.updateArtist(artist);
    }
 
    public void updateDVDPublicationDate(int id,String publicationDate)
    {
        DVD dvd=returnDVD(id);
        dvd.updatePublicationDate(publicationDate);
    }

    public int readArrayListSize()
    {
        return dvdRegister.size();
    }
    
    public DVD returnDVD(int id)
    {
        Iterator<DVD> it = dvdRegister.iterator();
        while(it.hasNext()) 
        {
            DVD dvd = it.next();
            if(id==dvd.readID())
                return dvd;
        }
        return null;
    }
    
    public void printDVD(int id)
    {
        for(DVD dvd: dvdRegister)
            if(id==dvd.readID())
                {
                    System.out.println("ID: "+dvd.readID());
                    System.out.println("Title: "+dvd.readTitle());
                    System.out.println("Artist: "+dvd.readArtist());
                    System.out.println("PublicationDate: "+dvd.readPublicationDate());
                    if(dvd.readCopies().size()==0)
                     //   return 
                    System.out.println("Number of copies availble: "+dvd.readNumberOfAvilableCopies());
                    System.out.println("Total number of copies: "+dvd.readSize());
                }
    }
    
    
}
